package com.blackcat.mybatisplus.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blackcat.mybatisplus.wrapper.entity.Student;
import com.blackcat.mybatisplus.wrapper.entity.User;
import com.blackcat.mybatisplus.wrapper.mapper.StudentMapper;
import com.blackcat.mybatisplus.wrapper.mapper.UserMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

/**
* 
* @author zhanghui
* @since 2023/4/3 10:27
*/
@SpringBootTest
public class QueryWrapperExample {

    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private StudentMapper studentMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assertions.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

    @Test
    public void selectMaps() {
        System.out.println(("----- selectAll method test ------"));
        List<Map<String, Object>> list = userMapper.selectMaps(null);
        list.forEach(System.out::println);
    }

    @Test
    public void testSelect2() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("student_age");
        queryWrapper.select("distinct id","student_name as name");
        List<Student> userList = studentMapper.selectList(queryWrapper);
        userList.forEach(System.out::println);
    }

    @Test
    public void testSelect3() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("student_age");
        queryWrapper.select("student_name as name");
        List<Map<String, Object>> userList = studentMapper.selectMaps(queryWrapper);
        userList.forEach(System.out::println);
    }
}
